/**
 * Des.js
 */


const crypto = require('crypto');

/**
 * 填充
 * @param text
 * @returns {Buffer}
 */
function buf24(text) {
    let buf = Buffer.alloc(8);
    buf.write(text, 0)
    return buf;

}

/**
 * 加密
 * @param text  内容
 * @param key   密钥
 * @returns {*}
 */
module.exports.encrypt = function(text, key) {
    key = buf24(key);
    const cipher = crypto.createCipheriv('des-ecb', key,'');
    const encrypted = cipher.update(text, 'utf8', 'base64');
    return encrypted + cipher.final('base64');
}

/**
 * 解密
 * @param encryptedBase64
 * @param key
 * @returns {*}
 */
module.exports.decrypt = function (encryptedBase64, key) {

    key = buf24(key);
    const decipher = crypto.createDecipheriv('des-ecb', key, '');
    let decrypted = decipher.update(encryptedBase64, 'base64', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
};






